/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pessoas.serializacao;

import java.io.BufferedWriter;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.util.ArrayList;
import java.util.Scanner;
import javax.swing.JOptionPane;
import pessoas.interfaces.ILog;
import pessoas.model.Contato;
import pessoas.model.Usuario;

/**
 *
 * @author Bush
 */
public class serializacaoArquivo implements ILog {

    private File arquivo, arquivoInvalido, arquivoUsuarios,arquivoLog;

    public serializacaoArquivo() {
        arquivo = new File("src/pessoas/data/contatos.txt");
        arquivoInvalido = new File("src/pessoas/data/contatosInvalidos.txt");
        arquivoUsuarios = new File("src/pessoas/data/usuarios.txt");        
        arquivoLog = new File("src/pessoas/data/logs.txt");
    }

    public int tamanhoArquivo(String caminho) throws FileNotFoundException, IOException {   //Método para saber tamanho do arquivo

        File arquivoLeitura = new File(caminho);
        int numLinhas = 0;
        long tamanhoArquivo = arquivoLeitura.length();
        FileInputStream fs = new FileInputStream(arquivoLeitura);
        DataInputStream in = new DataInputStream(fs);

        LineNumberReader lineRead = new LineNumberReader(new InputStreamReader(in));
        lineRead.skip(tamanhoArquivo);
// conta o numero de linhas do arquivo, começa com zero, por isso adiciona 1 
        numLinhas = lineRead.getLineNumber() + 1;
        return numLinhas;

    }

    //-----------------------------Leitura-------------------------------------
    public ArrayList<Usuario> carregaUsuarios() throws FileNotFoundException {
        Scanner scan = new Scanner(arquivoUsuarios);
        ArrayList<Usuario> usuarios = new ArrayList<Usuario>();
        while (scan.hasNextLine()) {
            String linha = scan.nextLine();
            Scanner scanline = new Scanner(linha);
            scanline.useDelimiter(",");
            while (scanline.hasNext()) {
                String nomeUsuario = scanline.next();
                String senhaUsuario = scanline.next();
                usuarios.add(new Usuario(nomeUsuario, senhaUsuario));
            }
        }
        arquivoUsuarios.delete();
        return usuarios;
    }

    public ArrayList<Contato> carregaContato() throws FileNotFoundException {
        Scanner scan = new Scanner(arquivo);
        ArrayList<Contato> contatos = new ArrayList<Contato>();
        while (scan.hasNextLine()) {
            String linha = scan.nextLine();
            Scanner scanline = new Scanner(linha);
            scanline.useDelimiter(",");
            while (scanline.hasNext()) {
                String nome = scanline.next();
                String sobrenome = scanline.next();
                String telefone = scanline.next();
                String uf = scanline.next();
                String operadora = scanline.next();
                contatos.add(new Contato(nome, sobrenome, telefone, uf, operadora));
            }
        }
        arquivo.delete();
        return contatos;
    }

    public ArrayList<Contato> carregaContatoInvalido() throws FileNotFoundException {
        Scanner scan = new Scanner(arquivoInvalido);
        ArrayList<Contato> contatos = new ArrayList<Contato>();
        while (scan.hasNextLine()) {
            String linha = scan.nextLine();
            Scanner scanline = new Scanner(linha);
            scanline.useDelimiter(",");
            while (scanline.hasNext()) {
                String nome = scanline.next();
                String sobrenome = scanline.next();
                String telefone = scanline.next();
                String uf = scanline.next();
                String operadora = scanline.next();
                contatos.add(new Contato(nome, sobrenome, telefone, uf, operadora));
            }
        }
        arquivoInvalido.delete();
        return contatos;
    }

    //-----------------------------Gravação-------------------------------------
    public void gravaUsuario(Usuario u) throws IOException, Exception {
        FileWriter w = new FileWriter(arquivoUsuarios, true);
        BufferedWriter bf = new BufferedWriter(w);
        bf.write(u.toString());
        bf.newLine();
        bf.close();
    }

    public void limpaGravaUsuario(ArrayList<Usuario> u) throws IOException, Exception {
        FileWriter w = new FileWriter(arquivoUsuarios, false);
        BufferedWriter bf = new BufferedWriter(w);
        for (int i = 0; i < u.size(); i++) {
            bf.write(u.get(i).toString());
            bf.newLine();
        }
        bf.close();
    }

    public void gravaContato(Contato c) throws IOException, Exception {
        FileWriter w = new FileWriter(arquivo, true);
        BufferedWriter bf = new BufferedWriter(w);
        bf.write(c.toString());
        bf.newLine();
        bf.close();
    }

    public void limpaGravaContato(ArrayList<Contato> c) throws IOException, Exception {
        FileWriter w = new FileWriter(arquivo, false);
        BufferedWriter bf = new BufferedWriter(w);
        for (int i = 0; i < c.size(); i++) {
            bf.write(c.get(i).toString());
            bf.newLine();
        }
        bf.close();
    }

    public void gravaContatoInvalido(Contato c) throws IOException, Exception {
        FileWriter w = new FileWriter(arquivoInvalido, true);
        BufferedWriter bf = new BufferedWriter(w);
        bf.write(c.toString());
        bf.newLine();
        bf.close();
    }

    public void limpaGravaContatoInvalido(ArrayList<Contato> c) throws IOException, Exception {
        FileWriter w = new FileWriter(arquivoInvalido, false);
        BufferedWriter bf = new BufferedWriter(w);
        for (int i = 0; i < c.size(); i++) {
            bf.write(c.get(i).toString());
            bf.newLine();
        }
        bf.close();
    }

    @Override
    public void gravarLog(String log) {
        FileWriter w;
        try {
            w = new FileWriter(arquivoLog, true);
            BufferedWriter bf = new BufferedWriter(w);
            bf.write(log);
            bf.newLine();
            bf.close();
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());
        }
    }

}
